<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Http\Controllers\Admin\BaseController;

use App\Http\Requests\Admin\Shop\Option\CreateRequest;
use App\Http\Requests\Admin\Shop\Option\UpdateRequest;
use App\Repositories\OptionRepositoryEloquent;
use App\Models\Option;
use Breadcrumbs, Toastr;

class OptionController extends BaseController{

    protected $option;

    public function __construct(OptionRepositoryEloquent $option)
    {
        parent::__construct();
        $this->option = $option;
    }

    public function index(){
        $options = Option::get();
        return view('admin.shop.option.index',compact('options'));
    }

    public function create(){
        return view('admin.shop.option.create');
    }

    public function store(CreateRequest $request){

        $picurl = '';
        if($request->hasFile('picurl')){
            if ($request->file('picurl')->isValid()){
            //if (1){
                $images=$request->file('picurl'); //使用laravel 自带的request类来获取一下文件.
                $filedir="upload/admin/shop/option/"; //定义图片上传路径
                $imagesName=$images->getClientOriginalName(); //获取上传图片的文件名
                $extension = $images->getClientOriginalExtension();
                //$images->move($filedir,$imagesName); //使用move 方法移动文件.
                $newImagesName=md5(time()).random_int(5,5).".".$extension; //重新命名上传文件名字
                $images->move($filedir,$newImagesName); //使用move 方法移动文件.
                $picurl =$filedir.$newImagesName ;
            }else{
                Toastr::error('图标上传失败!');
                return redirect(route('admin.shop.option.create'));
            }
        }

        $result = Option::create([
            'name'=>$request['name'],
            'code'=>$request['code'],
            'currency_name'=>$request['currency_name'],
            'currency_unit'=>$request['currency_unit'],
            'exchange_rate'=>$request['exchange_rate'],
            'fluctuation'=>$request['fluctuation'],
            'fluctuation_price'=>$request['fluctuation_price'],
            'surplus_value'=>$request['surplus_value'],
            'stop_loss_value'=>$request['stop_loss_value'],
            'stop_loss_margin'=>$request['stop_loss_margin'],
            'transaction_cost'=>$request['handtransaction_cost_count'],
            'hand_count'=>$request['hand_count'],
            'times'=>$request['times'],
            'timesdescription'=>$request['timesdescription'],
            'description'=>$request['description'],
            'rules'=>$request['rules'],
            'icon'=>$picurl,
            'status'=>$request['status'],
            'sort'=>$request['sort'],
            
        ]);
        if(!$result){
            Toastr::error('期权添加失败!');
            return redirect(route("admin.shop.option.create"));
        }
        Toastr::success('期权添加成功!');
         return redirect('admin/shop/option');
        
    }

    public function edit($id){
        $option = Option::find($id);
        //dd($reply);
        return view("admin.shop.option.edit",compact('option','option'));
    }

    public function update(UpdateRequest $request, $id){
        //$type=$request['msgtype'];
        $data = [
            'name'=>$request['name'],
            'code'=>$request['code'],
            'currency_name'=>$request['currency_name'],
            'currency_unit'=>$request['currency_unit'],
            'exchange_rate'=>$request['exchange_rate'],
            'fluctuation'=>$request['fluctuation'],
            'fluctuation_price'=>$request['fluctuation_price'],
            'surplus_value'=>$request['surplus_value'],
            'stop_loss_value'=>$request['stop_loss_value'],
            'stop_loss_margin'=>$request['stop_loss_margin'],
            'transaction_cost'=>$request['transaction_cost'],
            'hand_count'=>$request['hand_count'],
            'times'=>$request['times'],
            'timesdescription'=>$request['timesdescription'],
            'description'=>$request['description'],
            'rules'=>$request['rules'],
            'status'=>$request['status'],
            'sort'=>$request['sort'],
        ];

        if($request->hasFile('picurl')){
            if ($request->file('picurl')->isValid()){
            //if (1){
                $images=$request->file('picurl'); //使用laravel 自带的request类来获取一下文件.
                $filedir="upload/admin/wechat/reply/"; //定义图片上传路径
                $imagesName=$images->getClientOriginalName(); //获取上传图片的文件名
                $extension = $images->getClientOriginalExtension();
                //$images->move($filedir,$imagesName); //使用move 方法移动文件.
                $newImagesName=md5(time()).random_int(5,5).".".$extension; //重新命名上传文件名字
                $images->move($filedir,$newImagesName); //使用move 方法移动文件.
                $data['icon'] =$filedir.$newImagesName ;
            }else{
                Toastr::error('图标上传失败!');
                return redirect(route('admin.shop.option.edit'));
            }
        }

        //$data = $request->all();
        //dd($data);
        $isAble = Option::where('id', '<>', $id)->where('name', $data['name'])->count();
        if($isAble) {
           $result= [
                'status' => false,
                'msg' => '名称已被使用'
            ];
            Toastr::error($result['msg']);
        }else{
            $res = Option::where('id','=',$id)->update($data);
            if(!$res) {
                $result= [
                    'status' => false,
                    'msg' => '期权更新失败'
                ];
                Toastr::error($result['msg']);
            }else{
                Toastr::success('期权更新成功');
            }
            
        }

        // if(!$result['status']) {
        //         Toastr::error($result['msg']);
        // } else {
        //     Toastr::success('回复更新成功');
        // }

        
        return redirect(route('admin.shop.option.edit', ['id' => $id]));
        //return redirect('admin/wechat/reply');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        $result = $this->option->delete($id);
        return response()->json($result ? ['status' => 1] : ['status' => 0]);
    }

}